package io.renren.controller;

import com.alibaba.fastjson.JSONArray;
import io.renren.entity.SysAreaEntity;
import io.renren.entity.VO.SysAreaEntityVO;
import io.renren.entity.VO.SysOfficeEntityVO;
import io.renren.service.SysAreaService;
import io.renren.utils.PageUtils;
import io.renren.utils.Query;
import io.renren.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


/**
 * 区域管理
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-06-14 10:24:14
 */
@RestController
@RequestMapping("sysarea")
public class SysAreaController {
	@Autowired
	private SysAreaService sysAreaService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sysarea:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<SysAreaEntity> sysAreaList = sysAreaService.queryList(query);

        for (SysAreaEntity sysAreaEntity : sysAreaList) {
            int subSysAreaCount=0;
            subSysAreaCount = sysAreaService.findSubCount(sysAreaEntity.getAreaId());
            if(subSysAreaCount>0){
                sysAreaEntity.setLeaf(false);  //是否子节点
                sysAreaEntity.setExPanded(true); //是否展开
            }else{
                sysAreaEntity.setLeaf(true);
                sysAreaEntity.setExPanded(false);
            }
        }

        System.out.println(sysAreaList.toString());
        int total = sysAreaService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(sysAreaList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{areaId}")
	@RequiresPermissions("sysarea:info")
	public R info(@PathVariable("areaId") Long areaId){
		SysAreaEntity sysArea = sysAreaService.queryObject(areaId);
		
		return R.ok().put("sysArea", sysArea);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("sysarea:save")
	public R save(@RequestBody SysAreaEntity sysArea){
		sysAreaService.save(sysArea);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("sysarea:update")
	public R update(@RequestBody SysAreaEntity sysArea){
		sysAreaService.update(sysArea);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("sysarea:delete")
	public R delete(@RequestBody Long[] areaIds){
		sysAreaService.deleteBatch(areaIds);
		
		return R.ok();
	}

	/**
	 * 树菜单
	 */
	@RequestMapping("/tree")
	@RequiresPermissions("sysarea:list")
	public R getTree(){
		SysAreaEntityVO sys = sysAreaService.getTree();
		JSONArray jsonArray = new JSONArray();
		jsonArray.add(sys);
		return R.ok().put("list",jsonArray);
	}
	/**
	 * 迁移区域
	 */
	@RequestMapping("/updatearea")
	@RequiresPermissions("sysarea:update")
	public R updateArea(HttpServletRequest request){
		String areaId = request.getParameter("areaId");
		String parentareaId = request.getParameter("parentareaId");
		sysAreaService.updateArea(areaId,parentareaId);
		return R.ok();
	}
	
}
